﻿/* ==============================================================================
 * 功能描述：SkillPanelSkillItem  
 * 
 * 创 建 者：郑镇泉
 * 
 * 创建日期：2016/4/14 15:28:07
 * ==============================================================================*/
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using ZZQFrameWork;

/// <summary>
/// 技能面板技能item
/// </summary>
public class SkillPanelSkillItem : MonoBehaviour
{
    public SkillPanelSkillInfo skillInfo;

    /// <summary>
    /// 技能图标
    /// </summary>
    private Image iconImage;

    /// <summary>
    /// 外框
    /// </summary>
    private Image frameImage;

    /// <summary>
    /// 锁图标
    /// </summary>
    private Image lockedImage;

    /// <summary>
    /// 解锁图标
    /// </summary>
    private Image unLockedImage;

    /// <summary>
    /// 解锁后未学习时的外框
    /// </summary>
    private Image selectFrameImage;
    private bool isLocked = true;

    void Awake()
    {
        iconImage = transform.GetComponentByPath<Image>("Icon");
        frameImage = transform.GetComponentByPath<Image>("Frame");
        lockedImage = transform.GetComponentByPath<Image>("Locked");
        unLockedImage = transform.GetComponentByPath<Image>("UnLocked");
        selectFrameImage = transform.GetComponentByPath<Image>("SelectFrame");
    }

    public bool IsLocked
    {
        get { return isLocked; }
        set
        {
            isLocked = value;
                SetLocked();
        }
    }

    private SkillVO data;

    public SkillVO Data
    {
        get { return data; }
        set
        {
            data = value;
            UpdateShow();
        }
    }

    /// <summary>
    /// 是否学习了该技能
    /// </summary>
    private bool isLearn = false;

    public bool IsLearn
    {
        get { return isLearn; }
        set
        {
            isLearn = value;
            if (isLearn)
            {
                SetLearn();
            }
        }
    }

    /// <summary>
    /// 设置学习
    /// </summary>
    private void SetLearn()
    {
        lockedImage.gameObject.SetActive(false);
        unLockedImage.gameObject.SetActive(false);
        selectFrameImage.gameObject.SetActive(false);
        frameImage.gameObject.SetActive(true);
    }

    private void UpdateShow()
    {
        iconImage.sprite = skillInfo.SkillAtlas.GetSprite(Data.icon);
        if (Data.lvNeed > ModuleManager.Instance.Get<CharacterModule>().mainRole.lv)
        {
            IsLocked = true;
        }
        else
        {
            IsLocked = false;
        }
    }

    /// <summary>
    /// 设置技能信息
    /// </summary>
    /// <param name="info"></param>
    public void SetSkillInfo(SkillPanelSkillInfo info)
    {
        skillInfo = info;
    }

    /// <summary>
    /// 解锁格子
    /// </summary>
    private void SetLocked()
    {
        if (!IsLocked)
        {
            lockedImage.gameObject.SetActive(false);
            unLockedImage.gameObject.SetActive(true);
        }
        else
        {
            lockedImage.gameObject.SetActive(true);
            unLockedImage.gameObject.SetActive(false);
        }
    }

    /// <summary>
    /// item点击
    /// </summary>
    /// <param name="click"></param>
    public void OnItemClicked(bool click)
    {
        if (!IsLocked&&!IsLearn)
        {
            selectFrameImage.gameObject.SetActive(click);
        }
        else
        {
            frameImage.gameObject.SetActive(click);
        }
    }
}